package com.dkha.server.common.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.dkha.common.enums.YNEnums;
import com.dkha.server.system.modules.security.user.SecurityUser;
import org.apache.ibatis.reflection.MetaObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

/**
 * 公共字段自动填充处理类
 */
public class MyMetaObjectHandler implements MetaObjectHandler {
    private static final Logger LOGGER = LoggerFactory.getLogger(MyMetaObjectHandler.class);


    @Override
    public void insertFill(MetaObject metaObject) {

        /**
         * Ps:此处不能用下划线表示数据库字段，要使用驼峰命名
         */

        Object isValidData = this.getFieldValByName(
                "isValid", metaObject);
        if (isValidData == null) {
            this.setFieldValByName("isValid", YNEnums.YES.code, metaObject);
        }
        this.setFieldValByName("createTime", new Date(), metaObject);
        this.setFieldValByName("createBy", String.valueOf(0L), metaObject);
        this.setFieldValByName("updateTime", new Date(), metaObject);
        this.setFieldValByName("updateBy", String.valueOf(0L), metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.setUpdateFieldValByName("updateTime", new Date(), metaObject);
        this.setUpdateFieldValByName("updateBy", String.valueOf(0L), metaObject);
    }
}

